package net.shopxx.action.member;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.bean.Pager;
import net.shopxx.bean.SystemConfig.PointType;
import net.shopxx.entity.AirOrder;
import net.shopxx.entity.CartItem;
import net.shopxx.entity.CartItemGroupbuy;
import net.shopxx.entity.CartItemProduct;
import net.shopxx.entity.DeliveryType;
import net.shopxx.entity.Groupbuy;
import net.shopxx.entity.Order;
import net.shopxx.entity.OrderItem;
import net.shopxx.entity.PaymentConfig;
import net.shopxx.entity.Product;
import net.shopxx.entity.ProductComment;
import net.shopxx.entity.Receiver;
import net.shopxx.service.DeliveryTypeService;
import net.shopxx.service.OrderItemService;
import net.shopxx.service.PaymentConfigService;
import net.shopxx.util.ArithUtil;
import net.shopxx.util.SystemConfigUtil;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 前台Action类 - 订单处理
 */

@ParentPackage("member")
@InterceptorRefs({
	@InterceptorRef(value = "token", params = {"excludeMethods", "info,list,view,pointdetail,pointexchange"}),
	@InterceptorRef(value = "memberStack")
})
public class OrderItemAction extends BaseMemberAction {
	
	private static final long serialVersionUID = 2553137844831167917L;
	
	private Boolean isSaveReceiver;// 是否保存收货地址
	private Integer totalQuantity;// 商品总数
	private Integer totalPoint;// 总积分
	private Double totalWeightGram;// 商品总重量（单位：g）
	private BigDecimal productTotalPrice;// 总计商品价格
	private String memo;// 附言
	
	private Receiver receiver;// 其它收货地址
	private DeliveryType deliveryType;// 配送方式
	private PaymentConfig paymentConfig;// 支付方式
	private Order order;// 订单
	private Set<CartItem> cartItemSet;// 购物车项
	private List<ProductComment> productCommentSet;

	@Resource
	private DeliveryTypeService deliveryTypeService;
	@Resource
	private PaymentConfigService paymentConfigService;
	@Resource
	private OrderItemService orderItemService;
	
	// 订单列表
	public String list() {

		if (pager == null) {
			pager = new Pager();
		}
		pager.setPageSize(4);
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(OrderItem.class);
//		detachedCriteria.add(Restrictions.eq("productitem.member.id", getLoginMember().getId()));
//		pager = orderItemService.findByPager(pager,detachedCriteria);
		pager = orderItemService.getOrderItemList(getLoginMember(),pager);
		return LIST;
	}
		
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	
	// 获取所有配送方式
	public List<DeliveryType> getAllDeliveryType() {
		return deliveryTypeService.getAll();
	}
	
	// 获取所有支付方式
	public List<PaymentConfig> getAllPaymentConfig() {
		return paymentConfigService.getAll();
	}

	public Boolean getIsSaveReceiver() {
		return isSaveReceiver;
	}

	public void setIsSaveReceiver(Boolean isSaveReceiver) {
		this.isSaveReceiver = isSaveReceiver;
	}

	public Integer getTotalQuantity() {
		return totalQuantity;
	}

	public void setTotalQuantity(Integer totalQuantity) {
		this.totalQuantity = totalQuantity;
	}

	public Integer getTotalPoint() {
		return totalPoint;
	}

	public void setTotalPoint(Integer totalPoint) {
		this.totalPoint = totalPoint;
	}

	public Double getTotalWeightGram() {
		return totalWeightGram;
	}

	public void setTotalWeightGram(Double totalWeightGram) {
		this.totalWeightGram = totalWeightGram;
	}

	public BigDecimal getProductTotalPrice() {
		return productTotalPrice;
	}

	public void setProductTotalPrice(BigDecimal productTotalPrice) {
		this.productTotalPrice = productTotalPrice;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public Receiver getReceiver() {
		return receiver;
	}

	public void setReceiver(Receiver receiver) {
		this.receiver = receiver;
	}

	public DeliveryType getDeliveryType() {
		return deliveryType;
	}

	public void setDeliveryType(DeliveryType deliveryType) {
		this.deliveryType = deliveryType;
	}

	public PaymentConfig getPaymentConfig() {
		return paymentConfig;
	}

	public void setPaymentConfig(PaymentConfig paymentConfig) {
		this.paymentConfig = paymentConfig;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public Set<CartItem> getCartItemSet() {
		return cartItemSet;
	}

	public void setCartItemSet(Set<CartItem> cartItemSet) {
		this.cartItemSet = cartItemSet;
	}

	public List<ProductComment> getProductCommentSet() {
		return productCommentSet;
	}

	public void setProductCommentSet(List<ProductComment> productCommentSet) {
		this.productCommentSet = productCommentSet;
	}
	
	
	

}